from pynput import keyboard
from pynput import mouse
import time
import threading
import queue
q = queue.Queue() #task 0,1,2,3,4  left right press release
is_pressed = False
need_right = False
is_4 = False
shift_release_co = 0

g_task = 0
def do_task():
    while True:
        task = q.get()
        print('got task', task)
        #time.sleep(0.05)
        
        if task == 0:
            mouse.Controller().click(mouse.Button.left,1)
            mouse.Controller().release(mouse.Button.left)            
            
        if task == 1:
            mouse.Controller().click(mouse.Button.right,1)
        
t = threading.Thread(target=do_task)
t.setDaemon(True)
t.start()

def on_press(key):
    print('pre', str(key))
    return

    global need_right, is_pressed, is_4, shift_release_co
    if is_pressed and 'ctrl' in str(key):
        is_pressed = False
        mouse.Controller().release(mouse.Button.left)            
        
    if  '<137>' == str(key):
        print('press')
        is_4 = True
        shift_release_co = 0
        
        
        
    if  '<136>' == str(key):
        print('set right')
        need_right = True
        shift_release_co = 0
        
              


def on_release(key):
    print('rele', key)
    return

    global is_4, shift_release_co, is_pressed, need_right
    if  '<138>' == str(key):
        print('left click')
        q.put(0)
        
    if is_4 or need_right:
        
        shift_release_co += 1
        print("signed", is_4, need_right, shift_release_co)
        
        if shift_release_co == 4:
            print('shift 4', is_pressed)
            if need_right:
                print('right click')
                need_right = False
                q.put(1)
                return
            
            is_4 = False
            
            if not is_pressed:
                is_pressed = True
                mouse.Controller().press(mouse.Button.left)
            else:
                is_pressed = False
                mouse.Controller().release(mouse.Button.left)            
            
    return
    
   
listener = keyboard.Listener(
    on_release=on_release,
    on_press=on_press,
    
)
listener.start()
import time
while True:
    time.sleep(100)